import 'package:flutter/material.dart';
import 'package:flutter_shop_z/model/category.dart';
import 'package:flutter_shop_z/pages/category_page/bloc/category_bloc.dart';
import 'package:flutter_shop_z/pages/category_page/bloc/category_id_bloc.dart';
import 'package:flutter_shop_z/pages/category_page/bloc/category_sub_bloc.dart';
import 'package:flutter_shop_z/service/service_method.dart';
import 'package:flutter_shop_z/config/service_url.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class LeftCategoryNav extends StatefulWidget {
  int categoryIndex = 0;
  LeftCategoryNav(this.categoryIndex);
  _LeftCategoryNavState createState() =>
      _LeftCategoryNavState(this.categoryIndex);
}

class _LeftCategoryNavState extends State<LeftCategoryNav> {
  List<CategoryBean> list = [];
  int listIndex = 0; //当前选择中一级分类的索引值
  _LeftCategoryNavState(this.listIndex);
  @override
  void initState() {
    _getCategory();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CategoryIdBloc, CategoryIdState>(
        builder: (context, state) {
      listIndex = state.categoryId;
      print("state.categoryId===>${state.categoryId}");
      return Container(
        width: ScreenUtil().setWidth(180),
        decoration: BoxDecoration(
            border: Border(
                bottom: BorderSide(width: 0.5, color: Colors.black26),
                right: BorderSide(width: 0.5, color: Colors.black26))),
        child: ListView.builder(
          itemCount: list.length,
          itemBuilder: (context, index) {
            return _leftInkWell(index);
          },
        ),
      );
    });
  }

  void _getCategory() async {
    await getNavigator().then((value) {
      setState(() {
        list.addAll(
            CategoryListBean.formJson((value['data'] as List).cast()).data);
        List childList = list[0].categorySubList;
        BlocProvider.of<CategoryBloc>(context)
            .add(CategoryChangeEvent(childList));
      });
    });
  }

  Widget _leftInkWell(int index) {
    bool isClick = false;
    isClick = (index == listIndex) ? true : false;
    if (isClick) {
      List childList = list[index].categorySubList;
      BlocProvider.of<CategoryBloc>(context)
          .add(CategoryChangeEvent(childList));
    }
    return InkWell(
        onTap: () {
          setState(() {
            listIndex = index;
          });
          BlocProvider.of<CategoryIdBloc>(context)
              .add(SetCategoryIdEvent(index));
          List childList = list[index].categorySubList;
          BlocProvider.of<CategoryBloc>(context)
              .add(CategoryChangeEvent(childList));
          if (listIndex != index) {
            BlocProvider.of<CategorySubBloc>(context)
                .add(SetCategorySubEvent(null));
          }
        },
        child: Container(
          height: ScreenUtil().setHeight(100),
          padding: EdgeInsets.only(left: 8),
          decoration: BoxDecoration(
              color: isClick ? Colors.black12 : Colors.white,
              border: Border(
                  bottom: BorderSide(width: 0.5, color: Colors.black12))),
          child: Row(
            children: <Widget>[
              Image.network(
                serviceUrl + list[index].categoryImg,
                width: ScreenUtil().setWidth(30),
              ),
              Text(
                list[index].categoryName,
                style: TextStyle(fontSize: ScreenUtil().setSp(30)),
              ),
            ],
          ),
        ));
  }
}
